package com.example.JcGQS.controller;


import com.example.JcGQS.core.a;
import com.example.JcGQS.pojo.dto.*;
import com.example.JcGQS.pojo.vo.*;
import com.example.JcGQS.service.GoodsService;
import com.example.JcGQS.util.RequestParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 末日
 * @ClassName GoodsContorller
 * @author 末日
 * @date 2023/11/21
 */
@RestController
@RequestMapping(value = "/GQS/goods", produces = MediaType.APPLICATION_JSON_VALUE)
public class GoodsContorller {

    @Autowired
    private GoodsService goodsService;

    /**
     * 新增物料
     *
     * @return
     */
    @PostMapping("/insert")
    public void insertGoods(@RequestBody InsertGoodsDTO insertGoodsDTO, HttpServletRequest request) {
//        System.out.println(insertGoodsDTO);
        String token = request.getHeader("Authorization");
        goodsService.insertGoods(insertGoodsDTO, token);
    }

    /**
     * 删除物料
     *
     * @return
     */
    @PostMapping("/delete")
    public void deleteGoods(@RequestBody DeleteGoodsDTO deleteGoodsDTO, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        goodsService.deleteGoods(deleteGoodsDTO, token);
    }

    /**
     * 修改物料
     *
     * @return
     */
    @PostMapping("/update")
    public void updateGoods(@RequestBody UpdateGoodsDTO updateGoodsDTO, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        goodsService.updateGoods(updateGoodsDTO, token);
    }

    /**
     * 获取物料列表
     *
     * @return
     */
    @PostMapping("/list")
    public DataRecordVo<GoodsListVo> goodsList(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return goodsService.goodsList(pageDTO, token);
    }

    /**
     * 搜索物料
     *
     * @return
     */
    @PostMapping("/search")
    public DataRecordsVO<GoodsListVo> searchGoods(@RequestBody SearchGoodsDTO searchGoodsDTO, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
       return goodsService.searchGoods(searchGoodsDTO, token);
    }


    /**
     * @return List<GoodsVo>
     * @Title category
     * @Description 所有物料分类
     */
    @PostMapping(value = "/category")
    public List<GoodsVo> category() {
        return goodsService.listCategory();
    }

    /**
     * @return List<GoodsVo>
     * @Title goodsName
     * @Description 物料名称
     */
    @PostMapping(value = "/name")
    public List<GoodsVo> goodsName(@RequestBody String param) {
        GoodsAttributeVo attributeVo = RequestParamUtil.resolveParamToBean(param, GoodsAttributeVo.class);
        return goodsService.listGoodsName(attributeVo);
    }

    /**
     * @return List<GoodsVo>
     * @Title specification
     * @Description 物料规格
     */
    @PostMapping(value = "/specification")
    public List<GoodsVo> specification(@RequestBody String param) {
        GoodsAttributeVo attributeVo = RequestParamUtil.resolveParamToBean(param, GoodsAttributeVo.class);
//        System.out.println(attributeVo);
        return goodsService.listSpecification(attributeVo);
    }

    /**
     * @return List<GoodsVo>
     * @Title model
     * @Description 物料型号
     */
    @PostMapping(value = "/model")
    public List<GoodsVo> model(@RequestBody String param) {
        GoodsAttributeVo attributeVo = RequestParamUtil.resolveParamToBean(param, GoodsAttributeVo.class);
        return goodsService.listModel(attributeVo);
    }

    /**
     * @return List<GoodsVo>
     * @Title unit
     * @Description 物料单位
     */
    @PostMapping(value = "/unit")
    public List<GoodsVo> unit() {
        return goodsService.listUnit();
    }


    @PostMapping("/a")
    public void aVoid(@RequestBody a a){
//        System.out.println(a.getAa());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime aa = LocalDateTime.parse(a.getAa(),formatter);
//        System.out.println(aa);
    }


}
